package Controller;

import entity.Health;
import entity.Organ;
import service.HealthService;
import service.impl.HealthServiceImpl;
import service.impl.OrganServiceImpl;
import service.OrganService;
import service.impl.HealthServiceImpl;
import util.Constants;
import util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/*
 *86180
 *2021/10/3
 *15:51
 */
@WebServlet("/health")
public class HealthController extends BaseController {
    public String showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageIndex = request.getParameter("pageIndex");
        if(pageIndex==null){
            pageIndex="1";
        }
        Page page=new Page(Integer.valueOf(pageIndex));

        HealthService healthService=new HealthServiceImpl();
        List<Health> healthList = healthService.showAllHealth(page);

        request.setAttribute("page", page);
        request.setAttribute("healthList", healthList);

        return Constants.FORWARD+"/list.jsp";

    }

    public String modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String projectname = request.getParameter("projectname");
        Integer organId = Integer.valueOf(request.getParameter("organId"));
        String deadlineX = request.getParameter("deadline");
        Double money = Double.valueOf(request.getParameter("money"));
        Integer status = Integer.valueOf(request.getParameter("status"));


        //收参以及日期格式转换
        Date deadline=null;
        try {
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
            deadline=simpleDateFormat.parse(deadlineX);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Health health=new Health(0,projectname,status,money, deadline, organId, null);
        HealthService healthService=new HealthServiceImpl();


        if(id==""){
            healthService.addHealth(health);

            return Constants.REDIRECT+"/health?method=showAll";
        }else{
            health.setId(Integer.valueOf(id));
            healthService.updateHealth(health);
            return Constants.REDIRECT+"/health?method=showAll";
        }

    }

    public String showAllOrgan(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrganService organService=new OrganServiceImpl();
        List<Organ> organList = organService.showAllOrgan();
        request.setAttribute("organList",organList);
        return Constants.FORWARD+"/edit.jsp";
    }

    public String show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));

        HealthService healthService=new HealthServiceImpl();
        Health health = healthService.showHealthById(id);
        request.setAttribute("health",health);

        OrganService organService=new OrganServiceImpl();
        List<Organ> organList = organService.showAllOrgan();
        request.setAttribute("organList",organList);

        return Constants.FORWARD+"/edit.jsp";
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        HealthService healthService=new HealthServiceImpl();
        int i = healthService.deleteHealth(id);
        response.getWriter().println(i);
    }



}
